package servlet;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Emp;
import service.EmpService;
import service.impl.EmpServiceImpl;
/*
 *  /emp/save
 *  /emp/update
 *  /emp/remove
 *  /emp/getEmp
 *  /emp/getAll
 */
@WebServlet("/emp/*")
public class EmpServlet extends HttpServlet{
	
	EmpService service = new EmpServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 两个方法，实现一个即可
		doPost(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.得到请求路径
		System.out.println("url:" + req.getRequestURL());
		System.out.println("uri:" + req.getRequestURI());
		System.out.println("path:" + req.getServletPath());
		System.out.println("pathinfo:" + req.getPathInfo());
		
		switch (req.getPathInfo()) {
		case "/save": save(req,resp);break;
		case "/update": update(req,resp);break;
		case "/remove": remove(req,resp);break;
		case "/getEmp": getEmp(req,resp);break;
		case "/getAll": getAll(req,resp);break;
		}
	}
	
	public void getAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String path = service.getAll();
		req.getRequestDispatcher(path).forward(req, resp);
	}
	

	public void getEmp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String empno = req.getParameter("empno");
		String path = service.getEmp(Integer.valueOf(empno));
		req.getRequestDispatcher(path).forward(req, resp);
	}
	
	public void remove(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String empno = req.getParameter("empno");
		String path = service.remove(Integer.valueOf(empno));
		req.getRequestDispatcher(path).forward(req, resp);
	}

	public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 接受参数
		String empno = req.getParameter("empno");
		String ename = req.getParameter("ename");
		String job = req.getParameter("job");
		String mgr = req.getParameter("mgr");
		String hiredate = req.getParameter("hiredate");
		String sal = req.getParameter("sal");
		String comm = req.getParameter("comm");
		String deptno = req.getParameter("deptno");
		// 封装
		Emp emp = new Emp();
		emp.setEmpno(Integer.valueOf(empno));
		emp.setEname(ename);
		emp.setJob(job);
		emp.setMgr(Integer.valueOf(mgr));
		emp.setHiredate(Date.valueOf(hiredate));
		emp.setSal(Integer.valueOf(sal));
		emp.setComm(Integer.valueOf(comm));
		emp.setDeptno(Integer.valueOf(deptno));
		//调用服务
		String path = service.update(emp);
		// 转发
		req.getRequestDispatcher(path).forward(req, resp);
	}

	public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 接受参数
		String empno = req.getParameter("empno");
		String ename = req.getParameter("ename");
		String job = req.getParameter("job");
		String mgr = req.getParameter("mgr");
		String hiredate = req.getParameter("hiredate");
		String sal = req.getParameter("sal");
		String comm = req.getParameter("comm");
		String deptno = req.getParameter("deptno");
		// 封装
		Emp emp = new Emp();
		emp.setEmpno(Integer.valueOf(empno));
		emp.setEname(ename);
		emp.setJob(job);
		emp.setMgr(Integer.valueOf(mgr));
		emp.setHiredate(Date.valueOf(hiredate));
		emp.setSal(Integer.valueOf(sal));
		emp.setComm(Integer.valueOf(comm));
		emp.setDeptno(Integer.valueOf(deptno));
		//调用服务
		String path = service.save(emp);
		// 转发
		req.getRequestDispatcher(path).forward(req, resp);
	}
}
